package com.dragonsoftbravo.sqlbuilder;

/**
 * Created by DI on 2016/5/15.
 */
public class SqlContext {
    static Context sqlContext;
    static Context mySqlContext = new Context(){

        @Override
        public String getTable(String tableName) {
            return "`"+tableName+"`";
        }

        @Override
        public String getColumn(String columnName) {
            return "`"+columnName+"`";
        }

        @Override
        public String getSqlValue(Object val) {
            if (val == null) {
                return "''";
            }
            if (val instanceof String) {
                return "'" + val + "'";
            } else {
                return String.valueOf(val);
            }
        }
    };
    public static void initContext(Context context){
        sqlContext = sqlContext;
    }

    private static Context getContext(){
        if(sqlContext == null){
            sqlContext = mySqlContext;
        }
        return mySqlContext;
    }

    public static String getTable(String tableName){
        return getContext().getTable(tableName);
    }

    public static String getColumn(String columnName){
        return getContext().getColumn(columnName);
    }

    public static String getSqlValue(Object val) {
        return getContext().getSqlValue(val);
    }
}
